/*
 * @Description:
 * @Autor: lizhengkun
 * @Date: 2021-08-10 11:01:23
 * @LastEditTime: 2021-08-13 09:05:09
 */
import { GetOrgTreeData, GetUserMenu, GetUserInfo } from '@/services/index';
import { store } from '@/redux';
import { CommonModelType } from './interface';

const DesignModel: CommonModelType = {
  namespace: 'common',
  state: {},
  effects: {
    // 清除所有common数据
    *clearAllCommonSaga(_, { put }: any) {
      yield put({
        type: 'clearCommon',
        payload: {},
      });
    },
    // 获取用户菜单
    *getUserMenuSaga(_, { call, put }) {
      const state = store.getState();
      if (state.common.userMenu) return;
      const resData = yield call(GetUserMenu);
      // resData.push({
      //   code: 'declare',
      //   id: '1111',
      //   name: '申报',
      //   orderNum: 3,
      //   parentId: '-1',
      //   path: '/declare',
      //   spread: false,
      //   children: [
      //     {
      //       children: [],
      //       code: 'apply',
      //       id: '114201',
      //       name: '学生劳务申报(全校)',
      //       orderNum: 111,
      //       parentId: '1111',
      //       path: '/apply',
      //       spread: false
      //     }
      //   ]
      // });
      yield put({
        type: 'saveUserMenu',
        payload: resData,
      });
    },
    // 获取组织树数据
    *getOrgTreeDataSaga(_, { call, put }) {
      const state = store.getState();
      if (state.common.orgTree) return;
      const resData = yield call(GetOrgTreeData);
      yield put({
        type: 'getOrgTree',
        payload: resData,
      });
    },
    // 获取用户信息
    *getUserInfoSaga(_, { call, put }) {
      const state = store.getState();
      if (state.common.userInfo) return;
      const resData = yield call(GetUserInfo);
      yield put({
        type: 'getUserInfo',
        payload: resData,
      });
    },
  },
  reducers: {
    clearCommon() {
      return {};
    },
    saveUserMenu(state, { payload }) {
      return { ...state, userMenu: payload };
    },
    getOrgTree(state, { payload }) {
      return { ...state, orgTree: payload };
    },
    getUserInfo(state, { payload }) {
      return { ...state, userInfo: payload };
    },
  },
};

export default DesignModel;
